package com.minsheng.epidemic_rep.service.impl;

import com.minsheng.epidemic_rep.dao.FeverClinicsDao;
import com.minsheng.epidemic_rep.entity.FeverClinics;
import com.minsheng.epidemic_rep.service.IFeverClinicsService;
import com.minsheng.epidemic_rep.util.GenerateCsv;
import com.minsheng.epidemic_rep.util.ZipUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Classname FeverClinicsServiceImpl
 * @Description 发热门诊病人数据定时上传
 * @Date 2020/9/28 16:19
 * @Created by You
 */
@Service("feverClinicsService")
public class FeverClinicsServiceImpl implements IFeverClinicsService {

    @Autowired
    private FeverClinicsDao feverClinicsDao;

    @Scheduled(cron = "0 50 15 * * ?")
    @Override
    public void createCsv() throws ParseException {

        System.out.println("hello!");

        //文件头名称（字段名）
        List<String> colName = new ArrayList<String>();
        colName.add("P900");
        colName.add("P6891");
        colName.add("P7501");
        colName.add("P7502");
        colName.add("P7000");
        colName.add("P4");
        colName.add("P5");
        colName.add("P6");//出生日期,date
        colName.add("P7");//年龄，int
        colName.add("P7503");
        colName.add("P13");
        colName.add("P801");
        colName.add("P802");
        colName.add("P803");
        colName.add("P7505");
        colName.add("P7506");//就诊日期,date
        colName.add("P7507");
        colName.add("P7526");
        colName.add("P7529");
        colName.add("P281");
        colName.add("P1");
        colName.add("P7508");//总费用，double


        //日期格式转换
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        //从数据库获取发热门诊病人数据
        List<FeverClinics> feverClinicsList = new ArrayList<FeverClinics>();

        //获取今天凌晨点和7天前时间参数
        String endTime = sdf1.format(new Date()) + " 00:00:00";
        Date endDateTime = sdf2.parse(endTime);//结束时间是今天下午5点

        //获取7天前时间参数
        Calendar cal = Calendar.getInstance();
        cal.setTime(endDateTime);
        cal.add(Calendar.DATE,-7);
        Date startDateTime = cal.getTime();//起始时间是七天前的凌晨
        String startTime = sdf2.format(startDateTime);
        System.out.println(startTime);

        feverClinicsList = feverClinicsDao.findAll(startTime,endTime);

        //测试数据
//        FeverClinics feve = new FeverClinics("123", "123", "123", "123", "123", "123", "123", new Date(), 000, "123", "123", "123", "123", "123", "123", new Date(), "123", "123", "123", "123", "123", 12345.678);
//        feverClinicsList.add(feve);

        System.out.println(feverClinicsList);

        //存放所有数据列表
        List<List<String>> allList = new ArrayList<List<String>>();


        for(FeverClinics feverClinics : feverClinicsList){
            //将查询到实体数据的属性拆开转成字符串存放到列表中
            List<String> list = new ArrayList<String>();
            list.add(feverClinics.getP900());
            list.add(feverClinics.getP6891());
            list.add(feverClinics.getP7501());
            list.add(feverClinics.getP7502());
            list.add(feverClinics.getP7000());
            list.add(feverClinics.getP4());
            list.add(feverClinics.getP5());
            list.add(feverClinics.getP6());//出生日期,String
//            if(feverClinics.getP6() == null){
//                list.add("-");
//            } else {
//                list.add(sdf1.format(feverClinics.getP6()));
//            }

//            list.add(feverClinics.getP7());//年龄，Integer
            if(feverClinics.getP7() == null){
                list.add("-");
            } else {
                list.add(String.valueOf(feverClinics.getP7()));
            }
            list.add(feverClinics.getP7503());
            list.add(feverClinics.getP13());
            list.add(feverClinics.getP801());
            list.add(feverClinics.getP802());
            list.add(feverClinics.getP803());
            list.add(feverClinics.getP7505());

            list.add(feverClinics.getP7506());//就诊日期,String
//            if(feverClinics.getP7506() == null){
//                list.add("-");
//            } else {
//                list.add(sdf2.format(feverClinics.getP7506()));
//            }
            list.add(feverClinics.getP7507());
            list.add(feverClinics.getP7526());
            list.add(feverClinics.getP7529());
            if(StringUtils.isEmpty(feverClinics.getP281())){
                list.add("-");
            } else {
                if("null".equals(feverClinics.getP281().replace(" ", ""))){
                    list.add("-");
                } else {
                    list.add(feverClinics.getP281());
                }
            }
            list.add(feverClinics.getP1());
//            list.add(feverClinics.getP7508());//总费用，double
            if(feverClinics.getP7508() == null){
                list.add("-");
            } else {
                list.add(String.valueOf(feverClinics.getP7508()));
            }
            //将拆分后的数据放入到总数据列表中
            allList.add(list);
        }

        //生成日期后缀
        String dateStr = sdf1.format(new Date());

        try {
            GenerateCsv.generateCsv("d:/frmzsj/","fever_" + dateStr,colName,allList);
        } catch (IOException e) {
            e.printStackTrace();
        }



    }



}
